package com.wang.kmp

import android.os.Bundle
import android.os.Handler
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.fragment.app.FragmentActivity
import com.bumptech.glide.Glide
import com.wang.kmp.shared.server.MyServer

class MainActivity : FragmentActivity() {

    init {
        MyServer.start(false)
    }

    private val baseUrl = "http://localhost:${MyServer.getPortAndBlocking()}"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

//        setContent {
//            App()
//        }

        val ll = LinearLayout(this).apply {
            layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            orientation = LinearLayout.VERTICAL
        }

        val iv = ImageView(this).apply {
            layoutParams = LinearLayout.LayoutParams(200, 200)
            setBackgroundColor(0xffeeeeee.toInt())
        }
        ll.addView(iv)

        val wv = WebView(this).apply {
            layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        }
        ll.addView(wv)
        setContentView(ll)
        wv.settingsDefault(this)
        Handler(mainLooper).postDelayed({
            wv.loadUrl("$baseUrl/api/v1/meditation-scene-category")
//            wv.loadUrl("http://192.168.31.221:8765")
//            wv.loadUrl("http://baidu.com")

            Glide.with(this@MainActivity).load("file:///android_asset/composeResources/app_server_kmp.shared.generated.resources/drawable/zk.png").into(iv)
        }, 2000)
    }
}

@Preview
@Composable
fun AppAndroidPreview() {
    App()
}